<?php
class ImportAction extends \Pinenut\Action {
	protected function run() {
		$timeStart = microtime ( true );
		$memoryStart = memory_get_usage ();
		
		$filename = '../data/export.json.gz';
		$data = file_get_contents ( $filename );
		$data1 = gzuncompress ( $data );
		unset ( $data );
		$data = json_decode ( $data1, true );
		unset ( $data1 );
		
		// на этот раз пробегаем по всему массиву, создаём пустышки для всех сущностей, без связей
		// и карту связей c минимальным весом (для каждого факта - вывод-значение, для каждого вывода - факт-значение)
		// потом бегаем по каждой сущности, расставляем уже готовые связи и сейвим одним инсертом
		echo "<pre>";
		
		$conclusions = [ ];
		$facts = [ ];
		$factsMap = [ ];
		$factsWeights = [ ];
		$conclusionsWeights = [ ];
		$i = 0;
		foreach ( $data as $dk => $conclusionData ) {
			$conclusion = new Conclusion ();
			$conclusion->title = $conclusionData ['result'];
			$conclusions [( string ) $conclusion] = $conclusion;
			
			foreach ( $conclusionData ['questions'] as $fact => $weight ) {
				if (! isset ( $factsMap [$fact] )) {
					$f = new Fact ();
					$f->title = $fact;
					$facts [( string ) $f] = $f;
					$factsMap [$fact] = ( string ) $f;
				} else {
					$f = $facts [( string ) $factsMap [$fact]];
				}
				$factsWeights [( string ) $f] [( string ) $conclusion] = $weight;
				$conclusionsWeights [( string ) $conclusion] [( string ) $f] = $weight;
			}
			;
			
			$memory = sprintf ( "%0.1f", (memory_get_usage ()) / 1024 / 1024 ) . "Mb";
			echo "$i - $memory\n";
			ob_flush ();
			$i ++;
		}
		unset ( $data );
		$i = 0;
		do {
			$unset = [ ];
			foreach ( $conclusions as $ckey => $conclusion ) {
				foreach ( $conclusionsWeights [( string ) $conclusion] as $key => $weight ) {
					$ref = $conclusion->facts [$key];
					$ref->sum += $weight*10;
					$ref->count += 10;
				}
				$conclusion->save ();
				$unset [] = $ckey;
				unset ( $conclusion );
				// unset ( $conclusions [$ckey] );
				$memory = sprintf ( "%0.1f", (memory_get_usage ()) / 1024 / 1024 ) . "Mb";
				echo "$i - $memory\n";
				ob_flush ();
				$i ++;
				if (0 == ($i % 10))
					break;
			}
			foreach ( $unset as $key ) {
				unset ( $conclusions [$key] );
			}
		} while ( count ( $conclusions ) );
		
		$i = 0;
		do {
			$unset = [ ];
			foreach ( $facts as $ckey => $fact ) {
				foreach ( $factsWeights [( string ) $fact] as $key => $weight ) {
					$ref = $fact->conclusions [$key];
					$ref->sum += $weight*10;
					$ref->count += 10;
				}
				$fact->save ();
				$unset [] = $ckey;
				unset ( $fact );
				$memory = sprintf ( "%0.1f", (memory_get_usage ()) / 1024 / 1024 ) . "Mb";
				echo "$i - $memory\n";
				ob_flush ();
				$i ++;
				if (0 == ($i % 10))
					break;
			}
			foreach ( $unset as $key ) {
				unset ( $facts [$key] );
			}
		} while ( count ( $facts ) );
		

		echo "done!";
		
		// отображаем телеметрию
		$memoryFinish = memory_get_usage ();
		$memoryPeak = memory_get_peak_usage ();
		$timeFinish = microtime ( true );
		echo sprintf ( "<pre>%0.2fKb/%0.2fKb, %1.2fms</pre>", ($memoryFinish - $memoryStart) / 1024, ($memoryPeak - $memoryStart) / 1024, ($timeFinish - $timeStart) * 1000 );
		
		exit ();
	}
}
